package com.sean.test

import com.xmh.code.PongApplication
import com.xmh.code.controller.PongController
import com.xmh.code.service.BusinessService
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.mockito.Mock
import org.mockito.MockitoAnnotations
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.web.reactive.server.WebTestClient

import static org.mockito.Mockito.when

@SpringBootTest(classes = [PongApplication])
class PongControllerTest {

    @Mock
    private BusinessService businessService;

    private WebTestClient webTestClient;

    @BeforeEach
    void setUp() {
        MockitoAnnotations.openMocks(this);
        PongController pongController = new PongController(businessService)

        webTestClient = WebTestClient.bindToController(pongController).build()
    }

    @Test
    void testRespondToPing_WithPermission_200() {
        // Mock RateLimiter to allow permission
        when(businessService.doSomething("testData")).thenReturn("world")

        webTestClient.get()
                .uri(uriBuilder -> uriBuilder.path("/pong").queryParam("data", "testData").build())
                .exchange()
                .expectStatus().isOk()
                .expectBody(String.class).isEqualTo("world")
    }

    @Test
    void testRespondToPing_WithoutPermission_429() {
        // Mock RateLimiter to deny permission
        when(businessService.doSomething("testData")).thenReturn("world");

        webTestClient.get()
                .uri(uriBuilder -> uriBuilder.path("/pong").queryParam("data", "testData").build())
                .exchange()
                .expectStatus().isEqualTo(429) // TOO_MANY_REQUESTS
                .expectBody(String.class).isEqualTo(null)
    }
}

